/*
 * Author: vdaras
 */

#include "../sdl/SDLException.h"
#include "Picture.h"
#include "Exception.h"
#include "../ResourceManager.h"

namespace gui
{


/**
 * Constrcutor initializes the Component part of the Picture and opens an image
 * located @ path 'picPath'. Then resizes the Component in order to fit the
 * image's size.
 *
 * @param picPath: path of the image on disc
 */

Picture::Picture(const std::string &name, const std::string &picPath)
:
Component(name, 0, 0)
{
    //try opening an image
    try
    {
        m_picRes = ResourceManager::GetInstance( )->GetImage( picPath.c_str( ) );

        m_pic = m_picRes->GetRawData( );
    }
    catch(sdl::Exception &exc)
    {
        //an error occurred
        throw Exception( "Cannot create Picture! Image probably doesn't exist!" );
    }

    int imageWidth = m_pic->GetWidth( );
    int imageHeight = m_pic->GetHeight( );

    m_visiblePart.w = imageWidth;
    m_visiblePart.h = imageHeight;

    //set Picture's size
    SetWidth( imageWidth );
    SetHeight( imageHeight );
    
    UpdateVisiblePart( );
}


/**
 * Constrcutor initializes the Component part of the Picture and opens an image
 * located @ path 'picPath'. Then resizes the Component in order to fit the
 * image's size.
 *
 * @param picPath: path of the image on disc
 */

Picture::Picture(const std::string &name, Container *parent, int x, int y, const std::string &picPath)
:
Component(name, parent, x, y, 0, 0)
{
    //try opening an image
    try
    {
        m_picRes = ResourceManager::GetInstance( )->GetImage( picPath.c_str( ) );

        m_pic = m_picRes->GetRawData( );
    }
    catch(sdl::Exception &exc)
    {
        //an error occurred
        throw Exception( "Cannot create Picture! Image probably doesn't exist!" );
    }

    int imageWidth = m_pic->GetWidth( );
    int imageHeight = m_pic->GetHeight( );

    m_visiblePart.w = imageWidth;
    m_visiblePart.h = imageHeight;

    //set Picture's size
    SetWidth( imageWidth );
    SetHeight( imageHeight );
    
    UpdateVisiblePart( );
}


Picture::~Picture()
{
    m_picRes->DecreaseReference( );
}


/**
 * Resizes this Label.
 *
 * @param width: new width.
 * @param height: new height.
 */

void Picture::Resize(int width, int height)
{
    
}



/**
 * Draws the picture on a surface.
 *
 * @param target: target surface.
 */

void Picture::Draw(sdl::Surface* target) const
{
    m_pic->Blit( GetX( ), GetY( ), target, &m_visiblePart );
}


/**
 * Updates the picture
 */

void Picture::Update()
{
}

};
